package com.ljsw.activity.ckpage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.ljsw.activity.R;
import com.ljsw.adapter.PKMoneyInfoAdapter;
import com.ljsw.application.App;
import com.ljsw.dialog.TimeOut;
import com.ljsw.entity.gpk.StockLive;
import com.ljsw.server.UpServer;
import com.ljsw.util.DialogManager;

import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeoutException;

import static com.ljsw.application.App.user;

/***
 * 查看核心金库明细代码
 */
public class CKMoneyInfoActivity extends Activity implements View.OnClickListener {
    private static final String TAG = "PKListInfoActivity";
    private Handler handler;// 声明一个hander
    private PKMoneyInfoAdapter PKMoneyInfoAdapter;

    private List<StockLive> StockLivelist = new ArrayList<StockLive>();// 数据源
    private String PKNo;
    private String PKNetResult;// 返回的字符传结果
    private DialogManager dmanager;//弹出框管理类
    private TimeOut timeout;
    private String Userid;
    private ListView ckmoney_info;// 组件listvew
    private ImageView ckmoneyinfoblack;//  返回
    private Button ckmoneyinfo_update;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ckmoney_info);
        PKNo = getIntent().getStringExtra("TaskNo");
        Userid = App.userZhanghao;
        initView();

        dmanager = new DialogManager(CKMoneyInfoActivity.this);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        timeout = dmanager.getTimeOut();//超时弹框
        Intent intent = new Intent();
        PKNo = getIntent().getStringExtra("TaskNo");
        Userid = App.userZhanghao;

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 0://登录成功后跳转 上门服务页面

                        if (StockLivelist.size() > 0) {

                            PKMoneyInfoAdapter = new PKMoneyInfoAdapter(StockLivelist, CKMoneyInfoActivity.this);
                            ckmoney_info.setAdapter(PKMoneyInfoAdapter);
                        } else {

                        }
                        PKMoneyInfoAdapter.notifyDataSetChanged();
                        Log.d("login用户role------------", user.getUserId());
                        break;
                    case 1://登录超时
                        Toast.makeText(CKMoneyInfoActivity.this, "正在登陆请检查网络", Toast.LENGTH_SHORT).show();
                        break;
                    case 2://异常加载，无法访问服务器
                        Toast.makeText(CKMoneyInfoActivity.this, "请求数据失败", Toast.LENGTH_SHORT).show();
                        break;

                    case 3://异常加载，无法访问服务器
                        Toast.makeText(CKMoneyInfoActivity.this, "请求超时", Toast.LENGTH_SHORT).show();
                        break;
                    case 20:
                        PKMoneyInfoAdapter.notifyDataSetChanged();
                        break;

                    default:
                        Toast.makeText(CKMoneyInfoActivity.this, "请检查网络服务", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };
    }


    private void initView() {
        ckmoney_info = (ListView) findViewById(R.id.ckmoney_info);
        ckmoneyinfoblack = (ImageView) findViewById(R.id.ckmoneyinfoblack);
        ckmoneyinfoblack.setOnClickListener(this);
        ckmoneyinfo_update = (Button) findViewById(R.id.ckmoneyinfo_update);
        ckmoneyinfo_update.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.ckmoneyinfoblack:
                CKMoneyInfoActivity.this.finish();

                break;

            case R.id.ckmoneyinfo_update:
                LoadData();
                break;
        }
    }

    private void LoadData() {
        new Thread(new Runnable() {
            public void run() {
                try {
                    if (null == PKNo || PKNo.equals("")) {
                        handler.sendEmptyMessage(4);//查无此人信息
                    } else {
                        Log.e(TAG, PKNo);
                        Log.e(TAG, Userid);
                        PKNetResult = UpServer.listCoreDetail(PKNo, Userid);
                        Log.e(TAG, "-----" + PKNetResult);
                        if (null == PKNetResult) {
                            handler.sendEmptyMessage(2);//查无此人信息
                        } else {
                            Gson gson = new Gson();
                            StockLive[] mTailZerotoEntity = gson.fromJson(PKNetResult, StockLive[].class);
                            StockLivelist.clear();//  清空
                            List arrList = Arrays.asList(mTailZerotoEntity);
                            StockLivelist = new ArrayList(arrList);

                            if (StockLivelist != null || StockLivelist.equals("")) {
                                handler.sendEmptyMessage(0);//验证成功 返回三个元素在user

                            } else {
                                handler.sendEmptyMessage(2);//查无此人信息
                            }
                        }
                    }
                } catch (SocketTimeoutException e) {
                    e.printStackTrace();
                    handler.sendEmptyMessage(3);//超时链接
                } catch (TimeoutException e) {
                    handler.sendEmptyMessage(2);//查无此人信息
                } catch (NullPointerException e) {
                    e.printStackTrace();
                    handler.sendEmptyMessage(2);//查无此人信息
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    handler.sendEmptyMessage(2);//不是数字
                } catch (Exception e) {

                    e.printStackTrace();
                    Log.e("b", "异常：：：" + e);
                    handler.sendEmptyMessage(2);
                }

            }
        }).start();

    }

    @Override
    protected void onResume() {
        super.onResume();
        LoadData();
    }
}


